/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evangelion.core.daoImpl;

import com.evangelion.core.bean.Usuario;
import com.evangelion.core.dao.InicioDao;
import com.evangelion.core.exceptions.NonexistentEntityException;
import com.evangelion.core.exceptions.PreexistingEntityException;
import com.evangelion.core.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import org.springframework.stereotype.Repository;
/**
 *
 * @author Kellycasma
 */
@Repository("inicioDao")
public class InicioDaoImpl extends GenericDaoImpl<Serializable> implements InicioDao, Serializable{
   
    public List<Usuario> findUsuariosEntities() {
        return findUsuariosEntities(true, -1, -1);
    }

    public List<Usuario> findUsuariosEntities(int maxResults, int firstResult) {
        return findUsuariosEntities(false, maxResults, firstResult);
    }

    private List<Usuario> findUsuariosEntities(boolean all, int maxResults, int firstResult) {
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Usuario.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Usuario findUsuarios(Long id) {
        em = getEntityManager();
        try {
            return em.find(Usuario.class, id);
        } finally {
            em.close();
        }
    }

    public int getUsuariosCount() {
        em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Usuario> rt = cq.from(Usuario.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    @Override
    public Object getUsuariobyUsername(String username) {
        em = getEntityManager();
        Query q = null;
        try {
            q = em.createNamedQuery("Usuarios.findByCuenta").setParameter("cuenta", username);

        } catch (javax.persistence.NoResultException pne) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "usuario no existe", " "));

        } finally {
            //S em.close();
        }
        return q.getSingleResult();
    }

    @Override
    public Usuario getExistUser(String cuenta, String contrasena) throws Exception {
        System.out.println("......................................................................");
        em = getEntityManager();
        System.out.println("cuenta:"+cuenta+"--contraseña:"+contrasena);
        Query q = em.createNamedQuery("Usuario.findExistUser").setParameter("cuenta", cuenta).setParameter("contrasena", contrasena);
        return (Usuario) q.getSingleResult();
    }
}
